#include "sg4c.h"
#include <time.h>
#include <math.h>



#define PI 3.14159f
#define signof(x)           ( (x) < 0 ? 1 : 1 )

float mx=0, my=0;
sg_font_t font;
void setup(){
    font = sgCreateFont("courier", 16);

    fprintf( stderr, "%d", font.height );
}

inline float distance(float ax, float ay, float bx, float by){
    return sqrtf( powf(ax-bx, 2) + powf(ay-by, 2) );
}

void draw(){
    sgFill( sgGray(255) );
    sgRect( 0, 0, sgWidth(), sgHeight() );

    const float diameter = 10;
    const float cx = sgWidth()/2, cy = sgHeight()/2;
    sgNoStroke();
    sgFill( sgHexRGB(0xDD3333) );
    sgEllipse( cx-diameter/2, cy-diameter/2, diameter, diameter );

    sgFill( sgHexRGB(0x3333DD) );
    sgEllipse( mx-diameter/2, my-diameter/2, diameter, diameter );

    sgStrokeWeight(.5);
    sgStroke( sgGray(0x33) );

    sgLine( mx, my, cx, cy );

    float dist = distance( mx, my, cx, cy );
    float cosine = (mx-cx)/dist;
    float sine = (my-cy)/dist;

    float theta = acosf( cosine );


    sgFill( sgHexRGBA(0x099) ) ;
    sgFont(font);

    char textbuf[128];
    int textheight = my + font.height;
    sprintf(textbuf, "  dist: %.2f", dist);
    sgText(textbuf, 15+mx, textheight );
    textheight += font.height;

    sprintf(textbuf, " theta: %.2f", theta);
    sgText(textbuf, 15+mx, textheight );
    textheight += font.height;

    sprintf(textbuf, "cosine: %.2f", cosine);
    sgText(textbuf, 15+mx, textheight );
    textheight += font.height;

    sprintf(textbuf, "  sine: %.2f", sine);
    sgText(textbuf, 15+mx, textheight );
    textheight += font.height;
}


//events
void mouseMoved(){
    //Mouse moved
    mx = sgMouse().x;
    my = sgMouse().y;

    sgRedraw();

}

void mousePressed(){
    //A mouse button is pressed
}

void mouseReleased(){
    //A mouse button is released
}

void mouseClicked(){
    //A mouse button is pressed and released
}

void mouseDragged(){
    //The mouse moves when being pressed
}

void mouseEntered(){
    //The mouse pointer enters the display frame
}

void mouseExited(){
    //The mouse pointer leaves the display frame
}

void keyReleased(){
    //A key is released
}

void keyPressed(){
    //A key is pressed
}

void keyTyped(){
    //A key is pressed and then released
}

int main(){
    sg_config_t config;
    sgInitConfig( &config );

    config.screen.width = 400;  //set the horizontal resolution of the frame
    config.screen.height = 400; //set the vertical resolution of the frame
    config.framerate = 0;      //set the per-second framerate


    config.smooth = 1; // uncomment this line to enable smoothing

    //------------------------------------------------------------------
    //DO NOT MODIFY CODE BELOW

    //setup callbacks
    config.setup = setup;
    config.draw = draw;

    config.mousePressed = mousePressed;
    config.mouseReleased = mouseReleased;
    config.mouseMoved = mouseMoved;
    config.mouseEntered = mouseEntered;
    config.mouseExited = mouseExited;
    config.mouseClicked = mouseClicked;
    config.mouseDragged = mouseDragged;

    config.keyReleased = keyReleased;
    config.keyPressed = keyPressed;
    config.keyTyped = keyTyped;

    //initilize the system
    sgSetup(&config);

    //run forever
    return sgRunForever();
}
